
var Dialogs = new Class({
	initialize: function()
	{
		this.dialogs = [];
		
	}, // end function initialize
	
	hide: function(dialog)
	{
		if (this.dialogs.test(dialog))
		{
			$(dialog).style.display = 'none';
			
		} // end if
		
	},
	
	show: function(dialog)
	{
		if (this.dialogs.test(dialog))
		{
			$(dialog).style.display = '';
			
		} // end if
		
	},
	
	close: function(dialog)
	{
		if (this.dialogs.test(dialog))
		{
			$(dialog).style.display = 'none';
			
		} // end if
		
	}, // end function close
	
	triggerContentsUpdated: function(c, dialog, event)
	{
		if (this.dialogs.test(dialog))
		{
			$(dialog).getElementsBySelector('.content')[0].innerHTML = c;
			
			if (event != null)
			{
				event['func'](c, dialog, event['atr']);
				
			} // end if
			
		} // end if
		
	}, // end function triggerContentsUpdated
	
	triggerTitleUpdated: function(t, dialog, event)
	{
		if (this.dialogs.test(dialog))
		{
			$(dialog).getElementsBySelector('.title')[0].innerHTML = t;
			
			if (event != null)
			{
				event['func'](c, dialog, event['atr']);
				
			} // end if
			
		} // end if
		
	}, // end function triggerTitleUpdated
	
	create: function(template)
	{
		if (this.dialogs.test(template.getID()))
		{
			var dialogObj = $(template.getID());
			
			makeWindowVisibleByElem(dialogObj);
			
			return;
			
		} // end if
		
		this.dialogs.extend([template.getID()]);
		
		// Check if the browser has support to make these kind of things!
		if (!document.createElement)
		{
			alert('Je browser ondersteunt niet het aanmaken van DHTML componenten! Update je browser.');
			
			return;
			
		} // end if
		
		// Check if the template is correct. If it isn't; quit
		if (template.check() == false)
			return;
		
		// Create the dialog.
		var dialog  = document.createElement('div');
		var title   = document.createElement('div');
		var resize  = document.createElement('div');
		var close   = document.createElement('div');
		var content = document.createElement('div');
		
		dialog.setAttribute('id', template.getID());
		
		content.className = 'content';
		dialog.className  = 'dialog';
		resize.className  = 'resize';
		title.className   = 'title';
		close.className   = 'close';
		
		if (window.ie)
			dialog.style.cssText = template.getStyle();
		else
			dialog.setAttribute('style', template.getStyle());
		
		content.innerHTML = template.getContent();
		
		if (template.canCloseByUser() == true)
		{
			title.innerHTML = '<table width="100%" cellspacing=0 cellpadding=0><tr><td>' + template.getTitle() + '</td><td align=right><a href="#" onClick="dialogClose(\'' + template.getID() + '\');">CLOSE</a></td></tr></table>';
			
		} // end if
		else
		{
			title.innerHTML = template.getTitle();
			
		} // end else
			
		dialog.appendChild(title);
		dialog.appendChild(content);
		
		if (template.isSizeFixed() == false)
			dialog.appendChild(resize);
		
		$('dialogs').appendChild(dialog);
		
		// Apply Dimensions
		var dialogObj  = $(template.getID());
		
		this.apply(dialogObj, template);
		
		var dialogSize = template.getSize();
		
		if (dialogSize['x'] != -1)
			dialogObj.style.width = dialogSize['x'] + 'px';
		
		if (dialogSize['y'] != -1)
			dialogObj.style.height = dialogSize['y'] + 'px';
		
		// Place the panels in the center.
		var location = template.getStartLocation(); // returns 2 elements: x and y location
		// Possible for X: Left, Center, Right, [numeric/pro cent value like 300/300px or 25%]
		// Possible for Y: Top, Center, Bottom, [numeric/pro cent value like 300/300px or 25%]
		
		var pos_left = 0;
		var pos_top = 0;
		
		var calibrating_x = 0;
		var calibrating_y = 0;
		
		var panel_width = 0;
		var panel_height = 0;
		
		if (dialogSize['x'] == -1) location[0] = 'center';
		if (dialogSize['y'] == -1) location[1] = 'center';
		
		// X location
		switch (location[0].toLowerCase())
		{
			case 'left':
				pos_left = '0px';
				
			break;
			
			case 'center':
				pos_left = '50%';
				calibrating_x = 0.5;
				
			break;
			
			case 'right':
				pos_left = '100%';
				calibrating_x = 1;
				
			break;
			
			default:
				
				pos_left = location[0];
				
			break;
			
		} // end switch
		
		// Y location
		switch (location[1].toLowerCase())
		{
			case 'top':
				pos_top = '0px';
				
			break;
			
			case 'center':
				pos_top = '50%';
				calibrating_y = 0.5;
				
			break;
			
			case 'bottom':
				pos_top = '100%';
				calibrating_y = 1;
				
			break;
			
			default:
				
				pos_top = location[1];
				
			break;
		} // end switch
		
		// Place the panels on the right positions.
		dialogObj.style.left = pos_left;
		dialogObj.style.top  = pos_top;
		
		// Calibrate X
		if (calibrating_x != 0)
		{
			if (dialogSize['x'] == -1)
				panel_width = dialogObj.style.width;
			else
				panel_width = dialogSize['x'];
			
			dialogObj.style.left = dialogObj.getLeft() - panel_width * calibrating_x - 2 + 'px';
		
			if (dialogObj.style.left.toInt() < 0)
				dialogObj.style.left = 0 + 'px';
			
		} // end if
		
		// Calibrate Y
		if (calibrating_y != 0)
		{
			if (dialogSize['y'] == -1)
				panel_height = dialogObj.style.height;
			else
				panel_height = dialogSize['y'];
			
			dialogObj.style.top = dialogObj.getTop() - panel_height * calibrating_y - 2 + 'px';
			
			if (dialogObj.style.top.toInt() < 0)
				dialogObj.style.top = 0 + 'px';
			
		} // end if
		
	}, // end function create
	
	apply: function(el, template)
	{
		el.onclick = makeWindowVisibleByEvent;
		
		if (template.isLocFixed() == false)
			el.makeDraggable({onComplete: makeWindowVisibleByElem, onDrag: checkDragCords, handle: el.getElementsBySelector('.title')[0]});
		
		if (template.isSizeFixed() == false)
			el.makeResizable({onComplete: makeWindowVisibleByElem, onDrag: checkSizeCords, handle: el.getElementsBySelector('.resize')[0]});
		
		if (template.isOnTop() == true)
			makeWindowVisibleByElem(el);
		
	} // end function
	
}); // end class Dialogs

// Other code =]
var winHeight, winWidth;

winWidth = Window.getWidth();
winHeight = Window.getHeight();

if (winWidth < 100 || winHeight < 100)
	retrieveDocumentDimensions();

winWidth = winWidth-2;
winHeight = winHeight-2;

function retrieveDocumentDimensions()
{
	var myWidth = 0, myHeight = 0;
	
	if(typeof(window.innerWidth) == 'number')
	{
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
		
	}
	else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
	{
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
		
	}
	else if(document.body && (document.body.clientWidth || document.body.clientHeight))
	{
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
		
	} // end if
	
	winWidth = myWidth;
	winHeight = myHeight;
	
} // end function

function checkSizeCords(el)
{
	if (winWidth < (el.style.width.toInt()))
		el.style.width = winWidth + 'px';
		
	if (winHeight < (el.style.height.toInt()))
		el.style.height = winHeight + 'px';
	
	makeWindowVisible(el);
	
} // end function

function checkDragCords(el)
{
	if (el.getLeft() > (winWidth - el.style.width.toInt()))
		el.style.left = (winWidth - el.style.width.toInt()) + 'px';
	
	if (el.getTop() > (winHeight - el.style.height.toInt()))
		el.style.top = (winHeight - el.style.height.toInt()) + 'px';
	
	if (el.getLeft() < 0)
		el.style.left = '0px';
	
	if (el.getTop() < 0)
		el.style.top = '0px';
	
	makeWindowVisible(el);
} // end function

makeWindowVisibleByElem = function(el)
{
	makeWindowVisible(el);
};

makeWindowVisibleByEvent = function(event)
{
	var event = new Event(event);
	makeWindowVisible(event.target);
};

makeWindowVisible = function (el)
{
	if (el.className != 'dialog')
		return;
		
	var all_draggables = $S('.dialog');
	var highest = 0;
	all_draggables.each(
		function (ele)
		{
			if (ele == el || el.className != 'dialog')
				return;
			
			if (highest < ele.style.zIndex)
				highest = ele.style.zIndex;
			
		}
	);
	
	if (el.style.display == 'none')
		el.style.display = '';
	
	el.style.zIndex = 1*highest+1;
};

dialogClose = function(dialog)
{
	chat.getClsDialog().close(dialog);
	
};

cimport_loaded('core.Dialogs');